<?php

/**
 * Description of MemberModel
 *
 * @author 阿明 symphp@foxmail.com
 */
namespace Home\Model;

class MemberModel extends \Think\Model{
    
    /**
     * 自动验证
     * 用户名的必填  2-16 唯一
     * 密码必填  6-16 
     * email 必填唯一
     * 手机号码 必填 唯一
     * 手机验证码的自动验证
     */
    
    protected $_validate = [
        ['username','require','用户名必填',self::EXISTS_VALIDATE,'',self::MODEL_INSERT],
        ['username','2,16','用户名长度不正确',self::EXISTS_VALIDATE,'length',self::MODEL_INSERT],
        ['username','','用户名已存在',self::EXISTS_VALIDATE,'unique',self::MODEL_INSERT],
        ['password','require','密码必填',self::EXISTS_VALIDATE,'',self::MODEL_INSERT],
        ['password','6,16','密码长度不正确',self::EXISTS_VALIDATE,'length',self::MODEL_INSERT],
        ['email','require','邮箱必填',self::EXISTS_VALIDATE,'',self::MODEL_INSERT],
        ['email','','邮箱已存在',self::EXISTS_VALIDATE,'unique',self::MODEL_INSERT],
        ['tel','require','手机号码必填',self::EXISTS_VALIDATE,'',self::MODEL_INSERT],
        ['tel','','手机号码已存在',self::EXISTS_VALIDATE,'unique',self::MODEL_INSERT],
        ['captcha','checkPhonecode','手机验证码不正确',self::EXISTS_VALIDATE,'callback',self::MODEL_INSERT],
        ['captcha','checkCode','验证码不正确',self::EXISTS_VALIDATE,'callback',self::MODEL_INSERT],
        array('username', 'require', '用户名必填', self::MUST_VALIDATE, '', 'login'),
        array('password', 'require', '密码必填', self::MUST_VALIDATE, '', 'login'),
        array('captcha','require','验证码必填',self::MUST_VALIDATE,'','login'),
        array('captcha','check_captcha','验证码错误',self::MUST_VALIDATE,'callback','login'),
    ];
    
    /**
     * 自动完成 加盐 注册时间
     */
    protected $_auto = [
        ['salt','Org\Util\String::randString',self::MODEL_INSERT,'function'],
        ['add_time',NOW_TIME,self::MODEL_INSERT],
    ];

    /**
     * 添加用户 加密
     */
    public function addMember(){
        $this->data['password'] = salt_password($this->data['password'], $this->data['salt']);
        return $this->add();
    }
    
    /**
     * 检验手机验证码
     */
    public function checkPhonecode($code){
        //从session取出验证码
        $sessioncode = session('TEL_CAPTCHA');
        session('TEL_CAPTCHA',null);
        return $code == $sessioncode;
    }
    
    /**
     * 验证码的验证
     * @param type $code
     * @return type true|false
     */
    public function checkCode($code){
        $verify = new \Think\Verify();
        return $verify->check($code);
    }
    
    public function addlogin(){
        
    }
 
}
